﻿using CubicFWNet.Data.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CubicFWNet.Data.SystemManager
{
    public class UserRepository : RepositoryBase<User>
    {
        public IEnumerable<User> LoadUsers(int pageindex, int pagesize)
        {
            return dbcontext.Users.OrderBy(u => u.Id).Skip((pageindex - 1) * pagesize).Take(pagesize);
        }

        public IEnumerable<User> LoadInRole(Guid roleId)
        {
            var result = from user in dbcontext.Users
                         where (
                             dbcontext.Relevances.Where(uo => roleId == uo.SecondId && uo.Type == "UserRole")
                             .Select(u => u.FirstId)
                             .Distinct()
                         )
                         .Contains(user.Id)
                         select user;
            return result;
        }

     
        public int GetUserCntInRole(Guid roleId)
        {
            return LoadInRole(roleId).Count();
        }
        public IEnumerable<User> LoadInRole(int pageindex, int pagesize, Guid roleId)
        {
            return LoadInRole(roleId).OrderBy(u => u.Id).Skip((pageindex - 1) * pagesize).Take(pagesize);
        }
    }
}
